<%@ include file="includes/header.jsp" %>

<%
	String s=request.getParameter("searchTerm");
	if (s == "") {
		s = "%";
	}
	Class.forName("com.mysql.jdbc.Driver");
	Connection con=DriverManager.getConnection("jdbc:mysql://127.0.0.1/kroketweb", "kroketweb", "password");
	
	// Generate MySQL compatible current date
	java.text.DateFormat dateFormat = new java.text.SimpleDateFormat(
			"yyyy-MM-dd");
	java.util.Date date = new java.util.Date();
	String sDate = dateFormat.format(date);

	// Build query
	Statement productStatement = con.createStatement();
	ResultSet rs = productStatement
			.executeQuery("SELECT p.product_id AS product_id, p.product_name AS product_name, p.product_stock AS product_stock, q.price_value AS price_value"
					+ " FROM product p"
					+ " INNER JOIN price q"
					+ " ON p.product_id = q.product_id"
					+ " WHERE q.price_sDate < '"
					+ sDate
					+ "'"
					+ " AND (q.price_eDate > '"
					+ sDate
					+ "' OR q.price_eDate IS NULL)"
					+ " AND product_name LIKE '%"+s+"%';");

%>
	<h1>Zoek op product</h1>
	<p>
		<form action="searchResult.jsp">
			Zoekterm <input type="text" id="searchField" value="<%= s %>" name="searchTerm" />
			<input type="submit" value="Zoek" />
		</form>
	</p>
	<br />
	<h1>Zoekresultaat</h1>
	<p>
		Je hebt gezocht op: <span id="searchTerm"><%= s %></span><br />
		We hebben de volgende producten gevonden.
	</p>
	<br />
	<table id="searchResult">
    	<thead>
    		<tr>
    			<th>Product</th>
    			<th>Prijs(Euro)</th>
    			<th>Op voorraad</th>
    		</tr>
    	</thead>
    	
    	<tbody>
    	<%
    	int i = 1;
		while(rs.next()) {
			int product_id = rs.getInt("product_id");
			String name = rs.getString("product_name");
			int stock = rs.getInt("product_stock");
			Float fPrice = rs.getFloat("price_value");

			String sPrice = fPrice.toString();
			String price = sPrice.replaceAll("\\.", "\\,");

			String values[] = price.split(",");
			if (values[1].length() == 1) {
				values[1] += "0";
			}

			price = values[0] + "," + values[1];
			
		%>
			<tr>
	        	<%
	        		if(stock > 0) {
	        			String className = "";
	        			boolean evenRow = false;
	        			
	        			if(i%2 == 0) {
	        				className= "class=\"evenRow\"";
	        				evenRow = true;
	        			}
	        			out.println("<td "+ className +" onclick=\"toggleOrderAmountField("+product_id+", "+evenRow+");\" style=\"cursor: pointer;\" id=\"product"+product_id+"\">"+ name +"</td>");
	        			out.println("<td "+ className +" onclick=\"toggleOrderAmountField("+product_id+", "+evenRow+");\" style=\"cursor: pointer;\" id=\"price"+product_id+"\">&euro; "+price+"</td>");
	        			out.println("<td class=\"available\" onclick=\"toggleOrderAmountField("+product_id+", "+evenRow+");\" style=\"cursor: pointer;\">Ja</td>");
	        		} else {
	        			String className = "";
	        			if(i%2 == 0) {
	        				className= "class=\"evenRow\"";
	        			}
	        			out.println("<td "+ className +">"+ name +"</td>");
	        			out.println("<td "+ className +">&euro; "+price+"</td>");
	        			out.println("<td class=\"unavailable\">Nee</td>");
	        		}
	        	%>
	        	<td id="amountField<%= product_id %>"></td>
	        </tr>
   		<%
   		++i;
		}
        %>
    	</tbody>
    </table>
<%@ include file="includes/footer.jsp" %>